package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;



    @Override
    // 根据公寓id获取公寓的详细信息
    public ApartmentDetailVo getApartmentDetail(Long id) {

        //根据公寓id查询公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //获取这个公寓的标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.listLabelInfoByApartmentId(apartmentInfo.getId());

        //获取这个公寓的图片列表
        List<GraphVo> graphVoList = graphInfoMapper.list(apartmentInfo.getId());

        //获取这个公寓的配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.listByApartmentId(apartmentInfo.getId());


        //计算这个公寓所有房间中最低租金
        List<RoomInfo> roomInfoList = roomInfoMapper.listByApartmentId(apartmentInfo.getId());
        BigDecimal minRent = roomInfoList.stream().map(RoomInfo::getRent).min(BigDecimal::compareTo).get();

        //ApartmentDetailVo
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        return apartmentDetailVo;
    }
}




